package sw6.visualschedule.notification;

import sw6.visualschedule.R;
import sw6.visualschedule.activitylist.VisualSchedule;
import sw6.visualschedule.entities.ScheduleActivity;
import sw6.visualschedule.persistence.ScheduleActivityRepo;
import tools.BundleHelper;
import tools.SingletonMediaPlayer;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;

import java.util.Date;

public class NotificationActivity extends Activity {

    private transient ScheduleActivity mActivity;
    
    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
        setContentView(R.layout.notification);
        
        final ScheduleActivityRepo service = new ScheduleActivityRepo(this);
        
        final Bundle extras = getIntent().getExtras();
        final int activityId = extras != null ? extras.getInt(BundleHelper.ACTIVITY_ID) : -1;
        
        if (activityId != -1) {
            mActivity = service.getById(activityId);
            mActivity.setInstanceDate(new Date(extras.getLong(BundleHelper.INSTANCE_DATE)));
            
            final Display display = getWindow().getWindowManager().getDefaultDisplay();
            
            final ImageView image = (ImageView)findViewById(R.id.notificationActivityImage);
            image.setImageBitmap(mActivity.getImageBitmapThumbnail(this, (int)(display.getWidth() * 0.8), (int)(display.getHeight() * 0.6)));
            
            final ImageButton acceptButton = (ImageButton)findViewById(R.id.acceptButton);
            final ImageButton dismissButton = (ImageButton)findViewById(R.id.dismissButton);
            
            acceptButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(final View view) {
                    startActivity(new Intent().setClass(NotificationActivity.this, VisualSchedule.class));
                    finish();
                }
            });
            
            dismissButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(final View view) {
                	SingletonMediaPlayer.getMediaPlayer().stop();
                	finish();
                }
            });
        }
    }
    
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
    	super.onWindowFocusChanged(hasFocus);
    	
    	if (hasFocus) {
    		//Play sound
    		final MediaPlayer mPlayer = MediaPlayer.create(this, R.raw.notification);
    		mPlayer.start();    		
    	}
    }
}